#include "MotionBlurTestNode.h"
#include "MotionBlurTestScene.h"
 

 
CMotionBlurTestNode::CMotionBlurTestNode(void):ISceneNode(Singleton<CMotionBlurTestScene>::Instance())
{
	x=x=DXUTGetD3D9BackBufferSurfaceDesc()->Width/2;
	y=0;

	         m_volicity=D3DXVECTOR2(0.0025,0.0f);
			  g_pFullScreenRenderTarget=NULL;
	           g_pFullScreenRenderTargetSurf=NULL;

			   m_pFullScreenLastFrameRenderTargetSurf= NULL;
				   m_pFullScreenLastFrameRenderTarget=NULL;

}


CMotionBlurTestNode::~CMotionBlurTestNode(void)
{

 
}

void CMotionBlurTestNode::OnUpdate( double fTime, float fElapsedTime )
{
	//x=DXUTGetD3D9BackBufferSurfaceDesc()->Width/2;
 // static float volicity=0.0f;
  ;//*fElapsedTime;

	D3DXVECTOR2 a=(D3DXVECTOR2(DXUTGetD3D9BackBufferSurfaceDesc()->Width/2,DXUTGetD3D9BackBufferSurfaceDesc()->Height/2))-D3DXVECTOR2(x,y);
	D3DXVec2Normalize(&a,&a);
	 
	m_volicity+=a*fElapsedTime;

	x+=m_volicity.x;
	y+=m_volicity.y;
  
	m_Block[0] = ScreenVectexRHW( x, y, 0.0f, 0.0f,D3DCOLOR_ARGB(255,255,0,0));
	m_Block[1] =	ScreenVectexRHW( 64+x, 0+y, 1.0f, 0.0f,D3DCOLOR_ARGB(255,255,0,0));
	m_Block[2] =	ScreenVectexRHW( 64+x, 64+y, 1.0f, 1.0f,D3DCOLOR_ARGB(255,255,0,0));
	m_Block[3] =	ScreenVectexRHW(0.0f+x, 64+y, 0.0f, 1.0f,D3DCOLOR_ARGB(255,255,0,0));


	for(int i = 0; i < 4; i++) {
		m_Block[i].m_x -= 0.5;
		m_Block[i].m_y -= 0.5;
	}


}

void CMotionBlurTestNode::OnD3D9FrameRender( float fElapsedTime )
{ 
	HRESULT hr;
	hr = DXUTGetD3D9Device()->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1|D3DFVF_DIFFUSE);
	//DXUTGetD3D9Device()->SetTexture(0, m_pTexture);
	LPDIRECT3DSURFACE9 pBackbufferSurface=NULL;;
	DXUTGetD3D9Device()->GetRenderTarget(0,&pBackbufferSurface);
	DXUTGetD3D9Device()->SetRenderTarget(0,g_pFullScreenRenderTargetSurf);

	DXUTGetD3D9Device()->Clear( 0, 0, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(  255,255, 255, 255), 1.0f, 0 );
	hr =  DXUTGetD3D9Device()->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_Block, sizeof(ScreenVectexRHW));



	//combine  

	DXUTGetD3D9Device()->SetRenderTarget(0,m_pFullScreenLastFrameRenderTargetSurf);
 	DXUTGetD3D9Device()->SetPixelShader(m_pPixelShader);
	DXUTGetD3D9Device()->SetTexture(0, g_pFullScreenRenderTarget  );
	DXUTGetD3D9Device()->SetTexture(1,m_pFullScreenLastFrameRenderTarget);


	DXUTGetD3D9Device()->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
	DXUTGetD3D9Device()->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);

	 DXUTGetD3D9Device()->SetRenderState(D3DRS_ZENABLE,FALSE);
	 DXUTGetD3D9Device()->SetRenderState(D3DRS_ZWRITEENABLE,FALSE);
	hr =  DXUTGetD3D9Device()->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_Vertex, sizeof(ScreenVectexRHW));

	DXUTGetD3D9Device()->SetTexture(0, 0);
	DXUTGetD3D9Device()->SetTexture(1,0);
	DXUTGetD3D9Device()->SetPixelShader(0);
	DXUTGetD3D9Device()->SetRenderTarget(0,pBackbufferSurface);
	DXUTGetD3D9Device()->SetTexture(0,m_pFullScreenLastFrameRenderTarget );
	hr =  DXUTGetD3D9Device()->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_Vertex, sizeof(ScreenVectexRHW));
   hr =  DXUTGetD3D9Device()->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_Block, sizeof(ScreenVectexRHW));
   	DXUTGetD3D9Device()->SetTexture(0, 0);
	DXUTGetD3D9Device()->SetTexture(1,0);
	SAFE_RELEASE(pBackbufferSurface);
}

bool CMotionBlurTestNode::Unload()
{
	SAFE_RELEASE(g_pFullScreenRenderTargetSurf);
	SAFE_RELEASE(g_pFullScreenRenderTarget);
	SAFE_RELEASE(m_pFullScreenLastFrameRenderTarget);
	SAFE_RELEASE(m_pFullScreenLastFrameRenderTargetSurf);
	SAFE_RELEASE(m_pPixelShader);

	return true;
}

bool CMotionBlurTestNode::Load()
{
 
	int w=DXUTGetD3D9BackBufferSurfaceDesc()->Width;
	int h=DXUTGetD3D9BackBufferSurfaceDesc()->Height;
	m_Vertex[0] = ScreenVectexRHW( 0, 0, 0.0f, 0.0f);
	m_Vertex[1] =	ScreenVectexRHW( w, 0, 1.0f, 0.0f);
	m_Vertex[2] =	ScreenVectexRHW( w, h, 1.0f, 1.0f);
	m_Vertex[3] =	ScreenVectexRHW(0.0f, h, 0.0f, 1.0f);


	for(int i = 0; i < 4; i++) {
		m_Vertex[i].m_x -= 0.5;
		m_Vertex[i].m_y -= 0.5;
	}


	//Block.png
	// Create a A8R8G8B8 render target texture.  This will be used to render 
	// the full screen and then rendered to the backbuffer using a quad.
	 ( D3DXCreateTexture( DXUTGetD3D9Device(), w, h,
		1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8,
		D3DPOOL_DEFAULT, &g_pFullScreenRenderTarget ) );
	// Store pointers to surfaces so we can call SetRenderTarget() later
	 ( g_pFullScreenRenderTarget->GetSurfaceLevel( 0, &g_pFullScreenRenderTargetSurf ) );




	 ( D3DXCreateTexture( DXUTGetD3D9Device(), w, h,
		 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8,
		 D3DPOOL_DEFAULT, &m_pFullScreenLastFrameRenderTarget ) );
	 // Store pointers to surfaces so we can call SetRenderTarget() later
	 ( m_pFullScreenLastFrameRenderTarget->GetSurfaceLevel( 0, &m_pFullScreenLastFrameRenderTargetSurf ) );
	  
	
	//Pixel Shader

	 HRESULT hr;
	 LPD3DXBUFFER Code = NULL;
	 LPD3DXBUFFER ErrorMsgs = NULL;

	 hr = D3DXCompileShaderFromFile(TEXT("Media\\TextOut.ps"), NULL, NULL, "ps_main", "ps_2_0", D3DXSHADER_DEBUG, &Code, &ErrorMsgs, &m_pConstantTable);

	 if((FAILED(hr)) && (ErrorMsgs != NULL))		 
	 {
		 MessageBoxA(0,(char*)ErrorMsgs->GetBufferPointer(),0,0);
		 return hr;
	 }

	 hr = DXUTGetD3D9Device()->CreatePixelShader((DWORD*)Code->GetBufferPointer(), &m_pPixelShader);
	 if(FAILED(hr))
	 {
		 MessageBoxA(0,("Fail CreatePixelShader"),0,0);
		 return hr;
	 }


	return true;
	 
}
 